#include "animation.hpp"
#include <stdbool.h>

#include "config.h"

int main(int argc, char *argv[]) {
  // Unused argc, argv
  (void)argc;
  (void)argv;

  // Initialize SDL
  if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    fprintf(stderr,
            "SDL could not be initialized!\n"
            "SDL_Error: %s\n",
            SDL_GetError());
    return 0;
  }

  // Initialize SDL image
  int flags = IMG_INIT_JPG | IMG_INIT_PNG;
  if ((IMG_Init(flags) & flags) != flags) {
    fprintf(stderr,
            "SDL_image could not be initialized with JPG and PNG support!\n"
            "SDL_image Error: %s\n",
            IMG_GetError());
    return 0;
  }

  // Disable compositor bypass
  if (!SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR, "0")) {
    fprintf(stderr, "SDL can not disable compositor bypass!\n");
    return 0;
  }

  // Create window
  SDL_Window *window = SDL_CreateWindow(
      "Flying plane SDL animation", SDL_WINDOWPOS_CENTERED,
      SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
  if (!window) {
    fprintf(stderr,
            "Window could not be created!\n"
            "SDL_Error: %s\n",
            SDL_GetError());
  } else {
    // Create renderer
    SDL_Renderer *renderer =
        SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    if (!renderer) {
      fprintf(stderr,
              "Renderer could not be created!\n"
              "SDL_Error: %s\n",
              SDL_GetError());
    } else {
      // Start animation
      Animation_start(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);

      // Destroy renderer
      SDL_DestroyRenderer(renderer);
    }

    // Destroy window
    SDL_DestroyWindow(window);
  }

  // Quit SDL_image
  IMG_Quit();

  // Quit SDL
  SDL_Quit();

  return 0;
}